import 'package:efood_multivendor/controller/auth_controller.dart';
import 'package:efood_multivendor/controller/coupon_controller.dart';
import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/images.dart';
import 'package:efood_multivendor/view/base/custom_app_bar.dart';
import 'package:efood_multivendor/view/base/no_data_screen.dart';
import 'package:efood_multivendor/view/base/not_logged_in_screen.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'widget/coupons_widget.dart';

class CouponScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bool _isLoggedIn = Get.find<AuthController>().isLoggedIn();
    if (_isLoggedIn) {
      Get.find<CouponController>().getCouponList();
    }

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: CustomAppBar(title: 'coupon'.tr),
      body: _isLoggedIn
          ? GetBuilder<CouponController>(builder: (couponController) {
              return couponController.couponList != null
                  ? couponController.couponList!.length > 0
                      ? RefreshIndicator(
                          onRefresh: () async {
                            await couponController.getCouponList();
                          },
                          child: Scrollbar(
                              child: SingleChildScrollView(
                            physics: AlwaysScrollableScrollPhysics(),
                            child: Center(
                                child: SizedBox(
                                    width: Dimensions.WEB_MAX_WIDTH,
                                    child: ListView.builder(
                                      itemCount:
                                          couponController.couponList?.length ??
                                              0,
                                      shrinkWrap: true,
                                      physics: NeverScrollableScrollPhysics(),
                                      padding: EdgeInsets.all(
                                          Dimensions.PADDING_SIZE_LARGE),
                                      itemBuilder: (context, index) {
                                        return CouponsWidget(
                                          couponCode:
                                              '${couponController.couponList![index].code} ',
                                          coupponOff:
                                              '${couponController.couponList![index].discount}${couponController.couponList![index].discountType == 'percent' ? '%' : Get.find<SplashController>().configModel?.currencySymbol} off',
                                          couponImage: Images.coup1,
                                          couponType: couponController
                                              .couponList![index].couponType,
                                        );
                                      },
                                    ))),
                          )),
                        )
                      : NoDataScreen(
                          text: 'no_coupon_found'.tr,
                          images: Images.coupons,
                        )
                  : Center(
                      child: CircularProgressIndicator.adaptive(
                      valueColor:
                          AlwaysStoppedAnimation<Color>(Color(0xffff2222)),
                      backgroundColor: Color(0xffff8022),
                    ));
            })
          : NotLoggedInScreen(),
    );
  }
}
